/**
 * txUtil.js
 *
 * @NAmdConfig ./configuration.json
 * @NApiVersion 2.x
 */
define(['N/record', 'N/search', './moment', './lodash'], function (record, search, moment, lodash) {

    function formatDateTimeByMarketplace(marketplace, datetime) {
        var trandate;
        if (marketplace == 'A1PA6795UKMFR9') { //DE
            trandate = moment(datetime.substring(0, 19), 'DD.MM.YYYY HH:mm:ss');
        } else if (marketplace == 'A1VC38T7YXB528') { //JP
            trandate = moment(datetime.substring(0, 19), 'YYYY/MM/DD HH:mm:ss');
        } else if (marketplace == 'ATVPDKIKX0DER') { //US
            trandate = moment(datetime, 'MMM D,YYYY h:m:s A z');
        } else if (marketplace == 'A1F83G8C2ARO7P') { //UK
            trandate = moment(datetime.substring(0, 19), 'D MMM YYYY HH:mm:ss');
        } else if (marketplace == 'A13V1IB3VIYZZH') { //FR
            trandate = moment(replaceFranchMonthShortName(datetime).substring(0, 19), 'D MM YYYY HH:mm:ss');
        } else if (marketplace == 'A1RKKUPIHCS9HS') { //ES
            trandate = moment(datetime.substring(0, 19), 'DD/MM/YYYY HH:mm:ss');
        } else if (marketplace == 'APJ6JRA9NG5V4') { //IT
            trandate = moment(datetime.substring(0, 21), 'DD/MMM/YYYY HH.mm.ss');
        }
        log.debug('trandate', JSON.stringify(trandate));
        return trandate;
    }

    function getNSItemIdBySellerSkuAndStoreId(sellerSku, storeId) {
        var itemId;
        search.create({
            type: 'kititem',
            columns: [],
            filters: [['custitem_platformsku', 'is', sellerSku],
                'and', ['custitem_platform_store_information', 'is', storeId]]
        }).run().each(function (result) {
            itemId = result.id;
            log.debug('itemId', itemId);
        });
        if (itemId) {
            return itemId;
        } else {
            throw "ERROR:seller sku:" + sellerSku + " not exist!";
        }
    }

    function replaceFranchMonthShortName(str) {
        return str.replace(/janv./g, '01')
            .replace(/févr./g, '02')
            .replace(/mars/g, '03')
            .replace(/avr./g, '04')
            .replace(/mai/g, '05')
            .replace(/juin/g, '06')
            .replace(/juil./g, '07')
            .replace(/août./g, '08')
            .replace(/sept./g, '09')
            .replace(/oct./g, '10')
            .replace(/nov./g, '11')
            .replace(/déc./g, '12');
    }

    function getFormattedAmount(amount, marketplace) {
        marketplace = marketplace.toLowerCase();
        var total;
        if (marketplace === 'amazon.de' || marketplace === 'amazon.fr' ||
            marketplace === 'amazon.it' || marketplace === 'amazon.es') { //DE,FR,IT,ES
            total = amount.replace(/,/g, '.');
        } else {
            total = amount.replace(/,/g, '');
        }
        return total;
    }

    return {
        getFormattedAmount: getFormattedAmount,
        formatDateTimeByMarketplace: formatDateTimeByMarketplace,
        getNSItemIdBySellerSkuAndStoreId: getNSItemIdBySellerSkuAndStoreId
    };
});